package cate.game.role.res.guild.po.my.skill;

import cate.common.table.d.GDFunc;
import cate.game.attr.FightAttr;
import cate.game.role.Role;
import cate.game.role.res.guild.boss.GuildFuncImpl;
import cate.game.role.res.guild.po.my.MyGuildFuncBase;
import cate.game.role.res.guild.po.my.skill.msg.GuildSkillDataResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import org.springframework.data.annotation.Transient;

import java.util.ArrayList;
import java.util.List;

@NoteClass("协会技能")
public class MyGuildSkillPO extends MyGuildFuncBase implements GuildFuncImpl {

    @NoteField(value = "普通技能")
    public GuildNormalSkill normal;

    @NoteField(value = "PVP技能")
    public GuildPVPSkill pvp;

    @Transient
    @JsonIgnore
    @NoteField(value = "不同类型的skill")
    List<GuildSkillOperation> skillList;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        skillList = new ArrayList<>();
        if (normal == null) {
            normal = new GuildNormalSkill();
        }
        normal.initialize(role);
        skillList.add(normal);

        if (pvp == null) {
            pvp = new GuildPVPSkill();
        }
        pvp.initialize(role);
        skillList.add(pvp);
    }

    public void gmRebuildAll() {
        try {
            skillList.stream()
                    .forEach(guildSkillOperation -> guildSkillOperation.gmRebuildAll());
            noticeUpdate();
        } catch (Exception e) {
            role.getGame().notice.message(role, "出错");
        }
        noticeUpdate();
    }

    public void upgradeSkill(byte type, int order, byte job) {
        if (!guildFuncOpenCheck()) {
            return;
        }
        skillList.stream()
                .filter(operation -> operation.match(type))
                .forEach(guildSkillOperation -> guildSkillOperation.upgrade(type, order, job, this));
        role.getBase().updatePower();
    }

    public void backSkill(byte type, byte job) {
        skillList.stream()
                .filter(operation -> operation.match(type))
                .forEach(guildSkillOperation -> guildSkillOperation.back(type, job, this));
        noticeUpdate();
        role.getBase().updatePower();
    }

    private void noticeUpdate() {
        role.sendNow(new GuildSkillDataResp(this));
    }

    @JsonIgnore
    public FightAttr getAttr(byte type, byte job) {
        GuildSkillOperation op = skillList.stream()
                .filter(operation -> operation.match(type))
                .findAny().orElseGet(null);
        if (op == null) {
            return null;
        }
        return op.onGetAttr(type, job, this);
    }

    public List<Integer> getSkill(byte type, byte job) {
        GuildSkillOperation op = skillList.stream()
                .filter(operation -> operation.match(type))
                .findAny().orElseGet(null);
        if (op == null) {
            return null;
        }
        return op.onGetSkill(type, job, this);
    }

    public void gmNormalHighestLevel(byte type) {
        try {
            skillList.stream()
                    .filter(operation -> operation.match(type))
                    .forEach(GuildSkillOperation::gmHighestLevel);
            noticeUpdate();
        } catch (Exception e) {
            role.getGame().notice.message(role, "出错");
        }
    }


    @Override
    public int getFuncId() {
        return GDFunc.GuildFunc.SKILL;
    }
}
